﻿using JNPF.Basics.Models;
using JNPF.Data;
using JNPF.Utils;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 流程引擎
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018.01.17 
    /// </summary>
    internal class FlowEngineService : DataFactory, FlowEngineIService
    {
        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<FlowEngineEntity>> GetList()
        {
            using (var db = this.Basic)
            {
                return await db.GetIQueryable<FlowEngineEntity>().OrderBy(o => o.SortCode).ThenBy(o => o.CreatorTime).ToListAsync();

            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<FlowEngineEntity> GetInfo(string id)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<FlowEngineEntity>(m => m.Id == id);
            }
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="enCode">流程编码</param>
        /// <returns></returns>
        public async Task<FlowEngineEntity> GetInfoByEnCode(string enCode)
        {
            using (var db = this.Basic)
            {
                return await db.GetEntityAsync<FlowEngineEntity>(m => m.EnCode == enCode && m.EnabledMark == 1);
            }
        }
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="whereLambda">条件</param>
        /// <returns></returns>
        public async Task<bool> IsExist(Expression<Func<FlowEngineEntity, bool>> whereLambda)
        {
            using (var db = this.Basic)
            {
                return (await db.GetCountAsync(whereLambda) > 0);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task Delete(FlowEngineEntity entity)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.DeleteAsync(entity);
                await db.DeleteAsync<FlowEngineVisibleEntity>(m => m.FlowId == entity.Id);
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="visibleList">可见范围</param>
        public async Task Create(FlowEngineEntity entity, List<FlowEngineVisibleEntity> visibleList)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.InsertAsync(entity);
                foreach (FlowEngineVisibleEntity flowEngineVisibleEntity in visibleList)
                {
                    flowEngineVisibleEntity.Id = CommonHelper.Guid;
                    flowEngineVisibleEntity.FlowId = entity.Id;
                    flowEngineVisibleEntity.SortCode = visibleList.IndexOf(flowEngineVisibleEntity);
                    flowEngineVisibleEntity.CreatorTime = DateTime.Now;
                    flowEngineVisibleEntity.CreatorUserId = UserProvider.Instance.Get().UserId;
                    await db.InsertAsync(flowEngineVisibleEntity);
                }
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="visibleList">可见范围</param>
        public async Task Update(FlowEngineEntity entity, List<FlowEngineVisibleEntity> visibleList)
        {
            using (var db = this.Basic)
            {
                await db.BeginTransactionAsync();
                await db.UpdateAsync(entity);
                await db.DeleteAsync<FlowEngineVisibleEntity>(m => m.FlowId == entity.Id);
                foreach (FlowEngineVisibleEntity flowEngineVisibleEntity in visibleList)
                {
                    flowEngineVisibleEntity.Id = CommonHelper.Guid;
                    flowEngineVisibleEntity.FlowId = entity.Id;
                    flowEngineVisibleEntity.SortCode = visibleList.IndexOf(flowEngineVisibleEntity);
                    db.Insert(flowEngineVisibleEntity);
                }
                await db.CommitTransactionAsync();
            }
        }
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(FlowEngineEntity entity)
        {
            using (var db = this.Basic)
            {
                return await db.UpdateAsync(entity);
            }
        }

    }
}
